//binary _search
#include "stdio.h"
#include <stdlib.h>
#include "iostream"
int cmp(const void *a, const void *b)
{
    return (*(int *)a - *(int *)b);
}
int binary_sreach(int *a, int n, int key)
{

    int low = 0;
    int high = n - 1;
    int mid = (low + high) / 2;
    while (low <= high)
    {
        if (*(a + mid) == key)
            return mid;
        else if (*(a + mid) > key)
        {
            high = mid - 1;
            mid = (low + high) / 2;
        }
        else if (*(a + mid) < key)
        {
            low = mid + 1;
            mid = (low + high) / 2;
        }
    }
    return -1;
}
int bs(int a[], int low, int high, int key)
{
    if (low > high)
        return -1;
    else
    {
        int mid = (low + high) / 2;
        if (a[mid] == key)
            return mid;
        else if (a[mid] < key)
        {
            bs(a, mid + 1, high, key);
        }
        else
        {
            bs(a, low, mid - 1, key);
        }
    }
}
void main()
{
    int a[] = {5, 1, 3, 2, 6, 4, 9, 8, 7, 0};
    qsort(a, 10, sizeof(a[0]), cmp);
    int k = binary_sreach(a, 10, 9);
    if (k)
        printf("%d\n", *(a + k));
    else
    {
        printf("error");
    }

    int b = bs(a, 0,9, 11);
    if (b > 0)
        printf("%d", *(a + b));
    else
    {
        printf("error");
    }
}